﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.IO;

public partial class SysManage_ContractGroupAndRolesGroup_ContractGroupAndRolesGroup : INTPage
{
    SqlHelp help = new SqlHelp();
    CommonMethod comHelp = new CommonMethod();

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        if (!IsPostBack)
        {
            //权限
            comHelp.GetRightUnite(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), "ContractGroupAndRolesGroup" }, new string[] { "Button1", "Button2", "Button3", "Button4" }, new int[] { 1, 3, 3, 1 }, CommonModel.loginOut2);

            //绑定页面组
            BindGroup();
        }
    }

    #region 初始化绑定 WebAndGroup(string UserCode)    
    // 绑定角色组
    protected void BindGroup()
    {
        string StrWebGroupSql = " select CG.CGCode,CG.CGName from B_ContractGroup CG where CG.DeleteFlag<>1 ";
        ddlGroupName.DataTextField = "CGName";
        ddlGroupName.DataValueField = "CGCode";
        ddlGroupName.DataSource = help.GetDataTable(StrWebGroupSql);
        ddlGroupName.DataBind();

        NotInGroupBind(ddlGroupName.SelectedValue.ToString());
    }

    #endregion

    //分别绑定未在此页面角色组组的字段和在页面角色组的字段的列表
    protected void NotInGroupBind(string GCode)
    {
        //未在组里的
        string StrOutGSql = " select  RG.RGCode,RG.RGName from B_RolesGroup RG where RG.RGCode not in (select CGRG.RGCode from B_ContractGroupAndRolesGroup CGRG where CGRG.CGCode='" + GCode + "')";
        lbxNotInGroup.DataSource = help.GetDataTable(StrOutGSql);
        lbxNotInGroup.DataValueField = "RGCode";
        lbxNotInGroup.DataTextField = "RGName";
        lbxNotInGroup.DataBind();

        //在组里的
        string StrInSql = " select  RG.RGCode,RG.RGName from B_RolesGroup RG join B_ContractGroupAndRolesGroup CGRG on RG.RGCode=CGRG.RGCode where CGRG.CGCode='" + GCode + "'";
        DataTable Dt = help.GetDataTable(StrInSql);
        lbxInGroup.DataSource = Dt;
        lbxInGroup.DataValueField = "RGCode";
        lbxInGroup.DataTextField = "RGName";
        lbxInGroup.DataBind();

        lbxInsert2.DataSource = Dt;
        lbxInsert2.DataValueField = "RGCode";
        lbxInsert2.DataTextField = "RGName";
        lbxInsert2.DataBind();
    }

    //根据页面组动态绑定
    protected void ddlWebGroupName_TextChanged(object sender, EventArgs e)
    {
        lbxInsert.Items.Clear();
        lbxInsert2.Items.Clear();
        lbxDelete.Items.Clear();
        NotInGroupBind(ddlGroupName.SelectedValue.ToString());
    }

    #region 分组
    //单项添加
    protected void Button1_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        //int InNum = lbxWebNotInGroup.Items.Count;
        for (int i = 0; i < lbxNotInGroup.Items.Count; i++)
        {
            if (lbxNotInGroup.SelectedIndex >= 0)
            {
                lbxInGroup.Items.Add(lbxNotInGroup.SelectedItem);
                //是否插入
                int INum = 0;
                string StrColumn = lbxNotInGroup.SelectedItem.Text.ToString();

                for (int d = 0; d < lbxInsert2.Items.Count; d++)
                {
                    if (StrColumn.Equals(lbxInsert2.Items[d].Text.ToString()))
                    { ++INum; }
                }

                if (INum < 1)
                { lbxInsert.Items.Add(lbxNotInGroup.SelectedItem); }

                lbxNotInGroup.Items.Remove(lbxNotInGroup.SelectedItem);
                lbxDelete.Items.Remove(lbxNotInGroup.SelectedItem);
            }
        }
    }

    //单项移除
    protected void Button2_Click(object sender, EventArgs e)
    {
        //不在组里的页面数
        //int InNum = lbxWebInGroup.Items.Count;
        for (int i = 0; i < lbxInGroup.Items.Count; i++)
        {
            if (lbxInGroup.SelectedIndex >= 0)
            {
                lbxNotInGroup.Items.Add(lbxInGroup.SelectedItem);
                //插入行
                int INum = 0;
                //页面类型
                string ColumnStr = lbxInGroup.SelectedItem.Text.ToString();

                for (int k = 0; k < lbxInsert2.Items.Count; k++)
                {
                    if (ColumnStr.Equals(lbxInsert2.Items[k].Text.ToString()))
                    {
                        ++INum;
                    }
                }
                if (INum > 0)
                {
                    lbxDelete.Items.Add(lbxInGroup.SelectedItem);
                }
                lbxInsert.Items.Remove(lbxInGroup.SelectedItem);
                lbxInGroup.Items.Remove(lbxInGroup.SelectedItem);
            }
        }
    }

    //全部添加
    protected void Button4_Click(object sender, EventArgs e)
    {
        //在删除组的
        for (int j = 0; j < lbxDelete.Items.Count; j++)
        {
            lbxNotInGroup.Items.Remove(lbxDelete.Items[j]);
            lbxInGroup.Items.Add(lbxDelete.Items[j]);
        }
        //不在组里的页面数
        for (int i = 0; i < lbxNotInGroup.Items.Count; i++)
        {
            lbxInGroup.Items.Add(lbxNotInGroup.Items[i]);
            lbxInsert.Items.Add(lbxNotInGroup.Items[i]);
        }
        lbxNotInGroup.Items.Clear();
        lbxDelete.Items.Clear();
    }
    //全部删除
    protected void Button3_Click(object sender, EventArgs e)
    {
        //在添加组的
        for (int k = 0; k < lbxInsert.Items.Count; k++)
        {
            lbxNotInGroup.Items.Add(lbxInsert.Items[k]);
            lbxInGroup.Items.Remove(lbxInsert.Items[k]);
        }
        //不在组里的页面数
        for (int i = 0; i < lbxInGroup.Items.Count; i++)
        {
            lbxNotInGroup.Items.Add(lbxInGroup.Items[i]);
            lbxDelete.Items.Add(lbxInGroup.Items[i]);
        }
        lbxInGroup.Items.Clear();
        lbxInsert.Items.Clear();
    }
    #endregion

    //保存
    protected void cmdSave_Click(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl2);
        string Str = string.Empty;//操作字符串
        //页面编号
        string CGCode = ddlGroupName.SelectedItem.Value.ToString();
        //DataTable Dt = (DataTable)Session["InGroup_Dt"];
        //当前用户
        string UserName = Session["UserCode"].ToString();
        //当前日期
        string StrDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
        //统计插入与删除的行数
        int InsertN = lbxInsert.Items.Count;
        int DeleteN = lbxDelete.Items.Count;

        ////插入
        if (InsertN > 0)
        {   //插入SQL语句前部分
           string InsertStr = " Insert Into B_ContractGroupAndRolesGroup(DeleteFlag,AddAuthor,"
            + "AddDate,UpdateAuthor,UpdateDate,CGCode,RGCode) values('0','" + UserName
            + "','" + StrDate + "','" + UserName + "','" + StrDate + "','" + CGCode + "','";

            for (int i = 0; i < InsertN; i++)
            {
                Str += InsertStr + lbxInsert.Items[i].Value.ToString() + "');  ";
            }
        }
        //删除
        if (DeleteN > 0)
        {   //删除SQL语句前部分
            string DeleteStr = " Delete from B_ContractGroupAndRolesGroup where CGCode='"
                + CGCode + "' and RGCode='";
            for (int j = 0; j < DeleteN; j++)
            {
                Str += DeleteStr + lbxDelete.Items[j].Value.ToString() + "';   ";
            }
        }
        //string DeleteStr = string.Format("Delete from B_ContractGroupAndRolesGroup where RGCode='{0}'; ", CGCode);
        //InsertStr =string.Format(" Insert Into B_ContractGroupAndRolesGroup(DeleteFlag,AddAuthor,AddDate,UpdateAuthor,UpdateDate,CGCode,RGCode) values('0','{1}','{2}','{3}','{4}','{5}')",UserName,StrDate,UserName,StrDate,CGCode);

        if (!string.IsNullOrEmpty(Str))
        {
            if (help.RunSqlInt(Str) > 0)
            {
                CommonMethod.regAlertMessage("操作成功！", this);
                NotInGroupBind(ddlGroupName.SelectedItem.Value.ToString());
            }
        }
    }

    //查询
    protected void cmdQuery_Click(object sender, EventArgs e)
    {
        lbxNotInGroup.Items.Clear();
        //未在组里的
        string StrWNGSql = string.Format(" select  RG.RGCode,RG.RGName from B_RolesGroup RG where RG.RGCode not in ("
            + " select CGRG.RGCode from B_ContractGroupAndRolesGroup CGRG where CGRG.RGCode='{0}') and (RC.RGCode like '%{1}%' OR RC.RGName like '%{2}%')", ddlGroupName.SelectedValue.ToString(), txtCondition.Text.ToString(), txtCondition.Text.ToString());
        lbxNotInGroup.DataSource = help.GetDataTable(StrWNGSql);
        lbxNotInGroup.DataValueField = "RGCode";
        lbxNotInGroup.DataTextField = "RGName";
        lbxNotInGroup.DataBind();
    }
}